package com.tqyan.start.local;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

public class DataUpdate {

    public static void main(String[] args) throws Exception {
        role();
    }

    public static void sql() throws Exception {
        String sql = "update commodity_spu_info set commodity_code='%s' where commodity_id=%s and commodity_code='%s';";
        String sql2 = "update commodity_collect_task set commodity_code='%s' where commodity_id=%s and commodity_code='%s';";
        String sql3 = "update cluster_commodity_info set commodity_code='%s' where commodity_id=%s and commodity_code='%s';";
        Set<String> contains = new HashSet<>();
        List<String> readList = Files.readAllLines(Paths.get("/Users/chenchen/Desktop/sql.txt"), StandardCharsets.UTF_8);
        for (String read : readList) {
            if (contains.contains(read)) {
                continue;
            }
            contains.add(read);
            String[] arrays = read.split(",");
            String result = String.format(sql, arrays[2], arrays[0],arrays[1]);
            System.out.println(result);
            result = String.format(sql2, arrays[2], arrays[0],arrays[1]);
            System.out.println(result);
            result = String.format(sql3, arrays[2], arrays[0],arrays[1]);
            System.out.println(result);
        }
    }

    public static void role() throws Exception {
        Map<String, String> roleMap = new LinkedHashMap<>();
        List<String> readList = Files.readAllLines(Paths.get("/Users/chenchen/Desktop/temp.txt"), StandardCharsets.UTF_8);
        String curr = null;
        for (String read : readList) {
            if (read.contains("@@")) {
                curr = read.split("@@")[0];
                read = read.split("@@")[1];
                roleMap.put(curr, "");
            }
            if (read.length() > 1 && read.charAt(read.length() - 1) != '。') {
                read = read + "。";
            }
            String desc = roleMap.get(curr);
            if (null == desc) {
                desc = "";
            }
            roleMap.put(curr, desc + read);
        }

        List<Role> roleList = new ArrayList<>();
        int min = 9999;
        int max = 0;
        for (Map.Entry<String, String> role : roleMap.entrySet()) {
            String desc = role.getValue();
            // "
            if (desc.indexOf("\"") == 0) {
                desc = desc.substring(1, desc.length() - 2);
            }
            // [1]
            desc = desc.replaceAll(" *\\[[0-9\\-]+\\] *", "");
            // （156年）
            desc = desc.replaceAll(" *（[0-9]+年?） *", "");
            roleList.add(new Role(role.getKey(), desc));

//            System.out.println(role);
//            System.out.println(desc.length());
//            System.out.println();
            min = Math.min(min, desc.length());
            max = Math.max(max, desc.length());
        }
        System.out.println("min: " + min);
        System.out.println("max: " + max);
        System.out.println();

        // 排序
        roleList = roleList.stream().sorted(Comparator.comparing(Role::getLength)).collect(Collectors.toList());
//        for (Role role : roleList) {
////            if (role.getLength() < 300) {
////                continue;
////            }
//            System.out.println(role);
//        }
        for (Role role : roleList) {
            System.out.println(role.name);
        }
        System.out.println();
        for (Role role : roleList) {
            System.out.println(role.desc);
        }
    }

    private static class Role {
        private String name;
        private String desc;
        private int length;

        public Role(String name, String desc) {
            this.name = name;
            this.desc = desc;
            this.length = desc.length();
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public int getLength() {
            return length;
        }

        public void setLength(int length) {
            this.length = length;
        }

        @Override
        public String toString() {
            return name + "[" + length + "]: " + desc;
        }
    }

}


